// 组合两数只和 40
// candidates = [10,1,2,7,6,1,5], target = 8, 总数相加为8
var combinationSum = function (candidates, target) {
    let n = candidates.length;
    let res = [];
    let tmpPath = [];
    candidates = candidates.sort((a, b) => a - b);
    let backTrace = (tmpPath, target, start) => {
        if (target == 0) {
            res.push(tmpPath);
            return;
        }
        for (let i = start; i < n; i++) {
            if (target < candidates[i]) break;
            // 同层相同不做处理，避免重复项
            if (i > start && candidates[i - 1] == candidates[i]) continue;
            tmpPath.push(candidates[i])
            backTrace(tmpPath.slice(), target - candidates[i], i + 1);
            tmpPath.pop()
        }
    }
    backTrace(tmpPath, target, 0);
    return res;
}
console.log("求和", combinationSum([10, 1, 2, 7, 6, 1, 5], 8))
